﻿using System;
using Ext.Net;
using System.Collections.Generic;
using BG.Service;
using System.Web.Configuration;
using BG.Data;
using System.Linq;
using Kwan.Data;
using Kwan.Helper.Strings;

namespace BG.Web.Desktop
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Dictionary<string, object> cacheModule = UserUtil.GetModule(UserUtil.GetCookieRole());
            if (cacheModule == null) Response.Redirect("~/extlogin.aspx");

            Page.Title = WebConfigurationManager.AppSettings["title"];

            IList<TModule> list = (TModule.status > 0).List<TModule>();

            IList<TModule> CheckedModule = list.AsQueryable().Where(c => cacheModule.ContainsKey(c.NameEN)).ToList();

            //var Parent = list.AsQueryable().Where(a => CheckedModule.Any(b => b.Pid == a.EntityID)).ToList() as IList<TModule>;

            IList<TModule> Child = list.AsQueryable().Where(a => CheckedModule.Any(b => b.EntityID == a.EntityID && b.Pid > 0)).ToList();

            foreach (TModule t in Child)
            {
                MyDesktop.Shortcuts.Add(new DesktopShortcut() {  ShortcutID = t.NameEN, Text = t.NameCN, IconCls = "shortcut-icon icon48-" + t.NameEN });
                //MyDesktop.Modules.Add(CreateDM(t));
            }

            //房态信息
            //int checkInCount = (TOrder.status == 1).Count<TRoom>();
            
            
        }

        protected void RefreshDS(object sender, StoreRefreshDataEventArgs e)
        {
            IList<VOrder> orderList = (VOrder.status == 1).List<VOrder>();
            Store1.DataSource = orderList;
            Store1.DataBind();
        }

        private DesktopModule CreateDM(TModule t)
        {
            DesktopModule dm = new DesktopModule() { ModuleID = t.NameEN, WindowID = "win_" + t.NameEN };
            dm.Launcher.Text = t.NameCN;
            return dm;
        }

        [DirectMethod]
        public void EditPassword()
        {
            if (Page.User.Identity.IsAuthenticated)
            {
                TUser tt = (TUser.userName == Page.User.Identity.Name).ListFirst<TUser>();
                this.txtUserID.Text = tt.EntityID.ToString();
                txtUserName.Text = tt.UserName;
                winPassword.Show();
            }
            else
            {
                ExtHelper.ErrorMsg("访问错误", "请重新登录！", this.miEditPassword.ClientID);
            }

        }

        protected void SavePasswordWindow(object sender, DirectEventArgs e)
        {
            TUser tt = (TUser.userName == Page.User.Identity.Name).ListFirst<TUser>();
            if (MD5.Md5Decrypt(tt.Password) == this.txtOldPassword.Text)
            {
                tt.Password = MD5.Md5Encrypt(txtNewPassword.Text);
                DBContext.Save(tt);
                winPassword.Hide();
            }
            else
            {
                ExtHelper.ErrorMsg("验证错误", "旧密码错误！", this.winPassword.ClientID);
            }
        }

        protected void Logout_Click(object sender, DirectEventArgs e)
        {
            UserUtil.Logout();
            string returnUrl = Request.QueryString["returnUrl"];
            returnUrl = returnUrl != null ? returnUrl : "~/Ext/Default.aspx";
            Response.Redirect(returnUrl, true);
        }
    }
}